package com.drug.controller;

import com.drug.domain.DrugInformation;
import com.drug.domain.Result;
import com.drug.service.PurchasementService;
import com.drug.service.impl.PurchasementServiceImpl;
import com.drug.util.ExcelUtil;
import lombok.extern.log4j.Log4j;
import org.apache.logging.log4j.message.LoggerNameAwareMessage;
import org.apache.logging.log4j.spi.LoggerAdapter;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/purchasement")
public class PurchasementController {
    @Autowired
    private PurchasementService purchasementService;


    public static final Logger LOGGER = LoggerFactory.getLogger(PurchasementController.class);
    /**
     * 查询所以供货商
     * @return
     */
    @RequestMapping("/findAllghs")
    @ResponseBody
    public Result findAllSupplier(){

        Result result = null;
        try{
            result = purchasementService.findAllSupplier();
            LOGGER.info("PurchasementController------findAllghs   success");
        }catch (Exception e){
            LOGGER.info("PurchasementController------findAllghs   error");

        }


        return result;
    }

    /**
     * 查询所要添加的药品
     * @return
     */
    @RequestMapping("/findyaopin")
    @ResponseBody
    public Result findAllyappin(){

        return purchasementService.findyaopin();
    }

    /**
     * 向采购单中间表添加药品id
     * @return
     */
    @RequestMapping("/addyaopinid")
    @ResponseBody
    public Result addyapinid(@RequestParam Map<String,Object> map){

        return purchasementService.addyaopinid(map);
    }
    /**
     * 向退货单中间表添加药品id
     * @return
     */
    @RequestMapping("/addyaopinid1")
    @ResponseBody
    public Result addyapinid1(@RequestParam Map<String,Object> map){

        return purchasementService.addyaopinid1(map);
    }

    /**
     * 多条件查询采购单
     * @param map
     * @param pageNum
     * @return
     */
    @RequestMapping(value = "/findDI")
    @ResponseBody
    private Result findDI(@RequestParam Map<String,Object> map, @RequestParam(required=true,defaultValue="1")Integer pageNum){
        return  purchasementService.findAllPM(map,pageNum);
    }

    /**
     * 添加采购单
     * @return
     */
    @RequestMapping("/addpurchase")
    @ResponseBody
    private Result addpurchase( @RequestParam Map<String,Object> map){

        return purchasementService.addpurchase(map);
    }
    /**
     * 添加采购单
     * @return
     */
    @RequestMapping("/addjiesuandan")
    @ResponseBody
    private Result addpurchase1( @RequestParam Map<String,Object> map){
        return purchasementService.addjiesuandan(map);
    }

    /**
     * 删除采购单
     * @param id
     * @return
     */
    @RequestMapping("/delectById")
    private Result delectById(Integer id){
        return purchasementService.delectById(id);
    }

    /**
     * 根据id查询采购单
     * @param id
     * @return
     */
    @RequestMapping("/findById")
    @ResponseBody
    private Result findById(Integer id){
        return purchasementService.findById(id);
    }

    /**
     *
     *
     * 修改采购表
     * @param map
     * @return
     */
    @RequestMapping("/updatalala")
    @ResponseBody
    private Result updatalala(@RequestParam Map<String,Object> map){
        return purchasementService.updatalala(map);
    }

    /**
     * 多条件查询采购单全部信息
     * @param map
     * @return
     */
    @RequestMapping("/findallcgd")
    @ResponseBody
    private Result findAllla(@RequestParam Map<String,Object> map,@RequestParam(required=true,defaultValue="1")Integer pageNum){
        return purchasementService.findAllla(map,pageNum);
    }

    /**
     * 修改信息表里的供货商
     * @param map
     * @return
     */
    @RequestMapping("/updateghs")
    @ResponseBody
    private Result updateghs(@RequestParam Map<String,Object> map){
        return purchasementService.updateghs(map);
    }

    /**
     * 修改/添加采购量
     * @param
     * @return
     */
    @RequestMapping("/updatecgl")
    @ResponseBody
    private Result updatecgl(Integer[] diId,Integer[] caigoulaing,Integer podiId){
        return purchasementService.updatecgl(diId,caigoulaing,podiId);
    }

    /**
     * 多条件查询入库表信息
     * @param map
     * @return
     */
    @RequestMapping("/findAllrkb")
    @ResponseBody
    private Result findAllrkb(@RequestParam Map<String,Object> map,@RequestParam(required=true,defaultValue="1")Integer pageNum){
        return purchasementService.findAllrkb(map,pageNum);
    }


    /**
     *
     * @param map
     * @param
     * @return
     */
    @RequestMapping("/addruku")
    @ResponseBody
    private Result addruku(@RequestParam Map<String,Object> map){
        return purchasementService.addruku(map);
    }


    @RequestMapping(value = "/export")
    public String export(HttpServletRequest request, HttpServletResponse response) throws Exception {

        String message = "";

        //获取数据
        List<DrugInformation> list = purchasementService.selectPs1();

        //excel标题
        String[] title = {"采购单编号","采购单名称","流水号","通用名","商品名","剂型","规格","单位","转换系数","中标价","采购量","采购金额"};

        //excel文件名
        String fileName = "sampleInfo"+System.currentTimeMillis()+".xls";

        //sheet名
        String sheetName = "学生信息表";

        String [][] content = new String[list.size()][];
        for (int i = 0; i < list.size(); i++) {
            content[i] = new String[title.length];
            DrugInformation stu = list.get(i);

            content[i][0] = stu.getPurchaseOrder().getPoId()+"";
            content[i][1] = stu.getPurchaseOrder().getPoNumber()+"";
            content[i][2] = stu.getPurchaseOrder().getPoName()+"";
            content[i][3] = stu.getDiForm()+"";
            content[i][4] = stu.getDiUnit()+"";
            content[i][5] = stu.getDiCc()+"";
            content[i][6] = stu.getDiNewPrice()+"";
            content[i][7] = stu.getPodiPd().getPodiNumber()+"";
            content[i][8] = stu.getPodiPd().getPodiTotal()+"";

        }

        //创建HSSFWorkbook
        HSSFWorkbook wb = ExcelUtil.getHSSFWorkbook(sheetName, title, content, null);

        //响应到客户端
        try {
            this.setResponseHeader(response, fileName);
            OutputStream os = response.getOutputStream();
            wb.write(os);
            os.flush();
            os.close();
            message = "SUCCESS";
        } catch (Exception e) {
            e.printStackTrace();
            message = "ERROR";
        }
        return null;
    }

    //发送响应流方法
    public void setResponseHeader(HttpServletResponse response, String fileName) {
        try {
            try {
                fileName = new String(fileName.getBytes(),"utf-8");
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            response.setContentType("application/octet-stream;charset=utf-8");
            response.setHeader("Content-Disposition", "attachment;filename="+ fileName);
            response.addHeader("Pargam", "no-cache");
            response.addHeader("Cache-Control", "no-cache");
        } catch (Exception ex) {
            ex.printStackTrace();
        }   }
}

